package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐相关接口")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    /**
     * 分页查询套餐列表
     * @return
     */
    @GetMapping("/page")
    Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐列表:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation(value = "新增套餐")
    @CacheEvict(value = "setmealCache", key = "#setmealDTO.categoryId")
    public Result saveWithDishes(@RequestBody SetmealDTO setmealDTO){
        setmealService.saveWithDishes(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result delete(@RequestParam("ids") List<Long> ids){
        setmealService.delete(ids);
        return Result.success();
    }

    //页面回显
    @GetMapping("/{id}")
    @ApiOperation(value = "回显套餐")
    public Result<SetmealVO> getById(@PathVariable("id") Long id){//套餐id
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.udpate(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改套餐状态")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable("status")Integer status, Long id){
        setmealService.startOrStop(status, id);
        return Result.success();
    }

}
